package com.example.android_modularity_lab.ui.common

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.*
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.android_modularity_lab.R
import com.example.core.StringUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import java.lang.IllegalArgumentException

class MainActivity : AppCompatActivity() {
    companion object {
        val TABS = arrayOf("Fragment1", "Fragment2", "Fragment3")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val viewPager = findViewById<ViewPager2>(R.id.viewpager)
        viewPager.also { viewPager2 ->
            viewPager2.adapter = DemoCollectionPagerAdapter(this)
        }
        val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            tab.text = TABS[position]
        }.attach()
//        val navHostFragment =
//            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
//        val navController = navHostFragment.navController
//        val action = MainPart1FragmentDirections.actionMainPartFragment13ToMainPart2Fragment();
//        navController.navigate(action)
        val a = StringUtils.getAbc()
    }

}

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
class DemoCollectionPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {

    override fun getItemCount(): Int {
        return 3
    }

    override fun createFragment(position: Int): Fragment {
        return when (position) {
            0 -> MainPart1WrapFragment()
            1 -> MainPart2Fragment()
            2 -> MainPart3Fragment()
            else -> throw IllegalArgumentException()
        }
    }

}